<?php
session_start();
require_once("../models/Unit.php");
require_once("../models/Payment.php");
		$user="root";
		$password="";
		$database="whitehouse";
		mysql_connect(localhost,$user,$password);
		@mysql_select_db($database) or die( "Unable to select database");


		
		
		
		
		


		$unit_name="b7";
		$unit = new Unit;
		$unit->connect();
		$accountInfo = $unit->getAccountInformation($unit_name);

		
		$content = "Unit ".strtoupper($unit_name)."\r\n\r\n";
		$content .= "Lesse Name: ".$accountInfo['name']."\r\n";
		$content .="Home address: ".$accountInfo['home']."\r\n";
		$content .="Student Number: ".$accountInfo['studnum']."\r\n";
		$content .="Cellphone Number: ".$accountInfo['cellnum']."\r\n";
		$content .="Course: ".$accountInfo['course']."\r\n";
		$content .="College: ".$accountInfo['college']."\r\n";
		$content .="Classification: ".$accountInfo['classification']."\r\n";
		$content .="Parent's Name: ".$accountInfo['parent']."\r\n";
		$content .="Parent's Contact: ".$accountInfo['contact']."\r\n";
		$content .="Other Occupants: ".$accountInfo['occupants']."\r\n";

		$payment=new Payment;
		$row0=$payment->getPayments($unit_name);
		$total = 0;
		$i=0;
		
		
		$query1 = "SELECT monthly_rent from unit where unit_name='$unit_name'";
		$result1 = mysql_query($query1)
		or die(mysql_error());  
		$row1 = mysql_fetch_array($result1);
		
		
		$query = "SELECT month(start_date) m, year(start_date) y, day(start_date) d, start_date , deposit, previous_debts from unit where unit_name='$unit_name'";
		$result = mysql_query($query)
		or die(mysql_error());  
		$row = mysql_fetch_array($result);
		$year =  $row['y'];
		$month = $row['m'];
		$day = $row['d'];
		$deposit = $row['deposit'];
		$debts = $row['previous_debts'];

		
		$year2 = date("Y");
		$month2 = date("m");
		$day2 = date("d");
		
		$start = mktime(0,0,0,$month,$day,$year);
		$now = mktime(0,0,0,$month2,$day2,$year2);
		
		
		
		
		
		$content.= "\r\nStart Date: ".date("Y-m-d",$start)."\r\n\r";
		$content.= "Financial Information as of ".date("Y-m-d",$now)."\r\n";
		
		
		$content .="\r\n\r\nPayments Made\r\n\r\n";
		while($row0[$i]!=null){

				$content .= $row0[$i]['date_received']." * ";
				$content .=$row0[$i]['mode']." * ";
				$content .=$row0[$i]['receipt_no']." * ";
				$content .="P".$row0[$i]['amount']."\r\n";

			$total = $total + $row0[$i]['amount'];
			$i++;
		}		
		
		$content .="\r\nTotal Payments Made: $total\r\n";
		
		//BILLS
		
		
		
		$content.="\r\n\r\nBills\r\n";
		

		$compare_now = mktime(0,0,0,$month2-1,$day2+1,$year2);
		
		

		
		$a=$month;
		$c=$day;
		
		if($month<10)
			$a="0".$a;
			
		if($c<10)
			$c="0".$c;
		$month++;
		$d1=cal_days_in_month(CAL_GREGORIAN,date("m",$start),date("Y",$start));
		$total2= round(($d1-$day+1)/$d1*$row1['monthly_rent'],2);
		$totalwithpenalty=$total2;
		$balance=0;
		
		$content.="\r\n\r\nBill ** ".date("Y-m-$c",$start)." ** ";
		if($total2!=$row1['monthly_rent']){
			$content .= ($d1+1-$day) ." days for the month of ".date("M Y",$start)." ** ";
		}
		else{
			$content .= "Rent for ".date('M Y',$start)." ** ";
		}
		$content .= $total2." ** ";
		$balance = $total2;
		$content .= $balance."\r\n";
		
		
		$b = date("Y-m-17",$start);
		$penaltydate = $start;
		
		$query3 = "SELECT * from payment where unit = '$unit_name' and date_received between '".date("Y-m-01",$start)."' and '". date("Y-m-17",$start)."' order by date_received";
		$result3 = mysql_query($query3)
		or die(mysql_error());
		while($row3 = mysql_fetch_array($result3)){
			$amount = -$row3['amount'];
			$receipt_no = $row3['receipt_no'];
			$balance = $balance+$amount;
			
			$content .= "Payment ** ";
			$content .= $row3['date_received']." ** ";
			$content .= $receipt_no." ** ";
			$content .= -$amount." ** ";
			$content .= $balance."\r\n";

		}
		
		
		//Penalty
		$query2 = "SELECT sum(amount) paid from payment where unit='$unit_name' and date_received<='$b'";
				//echo $query2;
				$result2 = mysql_query($query2)
				or die(mysql_error());  
				$row2 = mysql_fetch_array($result2);
				
				if($totalwithpenalty-$row2['paid']>$deposit){
					$amount = min($row1['monthly_rent'],$totalwithpenalty-$row2['paid'])*.1;
					$balance = $balance + $amount;
					$content .= "Penalty ** ";
					$content .= date("Y-m-18",$penaltydate)." ** ";
					$content .= "10% of ".$amount*10 ." ** ";
					$content .= $amount." ** ";
					$content .= $balance."\r\n";
					$totalwithpenalty = $totalwithpenalty + $amount;
				}
				
		
		$query3 = "SELECT * from payment where unit = '$unit_name' and date_received between '".date("Y-m-18",$start)."' and '". date("Y-m-$d1",$start)."' order by date_received";
		$result3 = mysql_query($query3)
		or die(mysql_error());
		while($row3 = mysql_fetch_array($result3)){
		$amount = -$row3['amount'];
			$receipt_no = $row3['receipt_no'];
			$balance = $balance+$amount;
			$content .= "Payment ** ";
			$content .= $row3['date_received']." ** ";
			$content .= $receipt_no." ** ";
			$content .= -$amount." ** ";
			$content .= $balance."\r\n";

			}
			
			
			
			
			
			$start = mktime(0,0,0,$month++,1,$year);
		while($start<$compare_now){
			$d1=cal_days_in_month(CAL_GREGORIAN,date("m",$start),date("Y",$start));
			$amount = $row1['monthly_rent'];
			$content .= "Bill ** ";
			$content .=  date("Y-m-01",$start)." ** ";
			$content .=  "Rent for ".date('M Y',$start)." ** ";
			$content .=  $amount." ** ";
			


			
			$a = date("Y-m-17",$start);
			
			
			$penaltydate = $start;
			$query3 = "SELECT * from payment where unit = '$unit_name' and date_received between '".date("Y-m-01",$start)."' and '". date("Y-m-17",$start)."' order by date_received";
			$result3 = mysql_query($query3)
			or die(mysql_error());

			$query4 = "SELECT * from payment where unit = '$unit_name' and date_received between '".date("Y-m-18",$start)."' and '". date("Y-m-$d1",$start)."' order by date_received";
			$result4 = mysql_query($query4)
			or die(mysql_error());
			
			$start = mktime(0,0,0,$month++,1,$year);
			$total2 += $amount;
			$totalwithpenalty += $amount;
			
			$query2 = "SELECT sum(amount) paid from payment where unit='$unit_name' and date_received<='$a'";
			$result2 = mysql_query($query2)
			or die(mysql_error());  
			$row2 = mysql_fetch_array($result2);
			$balance=$balance+$amount;
			
			$content .=  $balance."\r\n";
			
			while($row3 = mysql_fetch_array($result3)){
				$amount = $row3['amount']*-1;
				$balance = round($balance + $amount);
				$content .= "Payment ** ";
				$content .= $row3['date_received']." ** ";
				$content .= $receipt_no." ** ";
				$content .= -$amount." ** ";
				$content .= $balance."\r\n";
			}
			
			
			if($totalwithpenalty-$row2['paid']>$deposit){
				$amount = min($row1['monthly_rent'],$totalwithpenalty-$row2['paid'])*.1;
				$balance = $balance + $amount;
					$content .= "Penalty ** ";
					$content .= date("Y-m-18",$penaltydate)." ** ";
					$content .= "10% of ".$amount*10 ." ** ";
					$content .= $amount." ** ";
					$content .= $balance."\r\n";
				$totalwithpenalty = $totalwithpenalty + $amount;
			}
			

			while($row4 = mysql_fetch_array($result4)){
				$amount = $row4['amount']*-1;
				$balance = round($balance + $amount);
				$content .= "Payment ** ";
				$content .= $row4['date_received']." ** ";
				$content .= $row4['receipt_no']." ** ";
				$content .= -$amount." ** ";
				$content .= $balance."\r\n";
			}
			
			
			if(date("m")==date("m",$start)){
				$amount =  $row1['monthly_rent'];
				$balance  = $balance + $amount;
				$d1=cal_days_in_month(CAL_GREGORIAN,date("m",$start),date("Y",$start));
				$content .= "Bill ** ";
				$content .=  date("Y-m-01",$start)." ** ";
				$content .=  "Rent for ".date('M Y',$start)." ** ";
				$content .=  $amount." ** ";
				$content .=  $balance."\r\n";
			
				$a = date("Y-m-17",$start);
				$penaltydate = $start;
				
				$query3 = "SELECT * from payment where unit = '$unit_name' and date_received between '".date("Y-m-01",$penaltydate)."' and '". date("Y-m-17",$penaltydate)."' order by date_received";
				$result3 = mysql_query($query3)
				or die(mysql_error());
				
				while($row3 = mysql_fetch_array($result3)){
					$amount = $row3['amount']*-1;
					$balance = round($balance + $amount);
					$content .= "Payment ** ";
					$content .= $row3['date_received']." ** ";
					$content .= $row3['receipt_no']." ** ";
					$content .= -$amount." ** ";
					$content .= $balance."\r\n";
				}
				
				$start = mktime(0,0,0,$month++,1,$year);
			$total2 += $row1['monthly_rent'];
			$totalwithpenalty += $row1['monthly_rent'];
			$outstanding = $total2-$total;
			$query2 = "SELECT sum(amount) paid from payment where unit='$unit_name' and date_received<='$a'";
			$result2 = mysql_query($query2)
			or die(mysql_error());  
			$row2 = mysql_fetch_array($result2);

			if($totalwithpenalty-$row2['paid']>$deposit&&date("d")>17){
				$amount = min($row1['monthly_rent'],$totalwithpenalty-$row2['paid'])*.1;
				$balance = $balance + $amount;
				$content .= "Penalty ** ";
				$content .= date("Y-m-18",$penaltydate)." ** ";
				$content .= "10% of ".$amount*10 ." ** ";
				$content .= $amount." ** ";
				$content .= $balance."\r\n";
				$totalwithpenalty = $totalwithpenalty + $amount;
			}
			
			$query3 = "SELECT * from payment where unit = '$unit_name' and date_received between '".date("Y-m-18",$penaltydate)."' and '". date("Y-m-$d1",$penaltydate)."' order by date_received";
			$result3 = mysql_query($query3)
			or die(mysql_error());
			
			while($row3 = mysql_fetch_array($result3)){
				$amount = $row3['amount']*-1;
				$balance = round($balance + $amount);
				$content .= "Payment ** ";
				$content .= $row3['date_received']." ** ";
				$content .= $row3['receipt_no']." ** ";
				$content .= -$amount." ** ";
				$content .= $balance."\r\n";
			}
				
				
			}
			
		}
		
		$content .= "Outstanding Balance: P". $balance."\r\n\r\n\r\n";
		$content .= "Deposit: P". $deposit."\r\n";
		$content .= "Total Debts Before Automation: P". $debts."\r\n";
		
		
		
		$content =addslashes($content);

$query = "INSERT INTO inactive (content ) ".
"VALUES ('$content')";

mysql_query($query) or die('Error, query failed');
?>